package com.property.cleaning.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.property.cleaning.common.Result;
import com.property.cleaning.entity.User;
import com.property.cleaning.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 * 
 * @author Property Cleaning System
 */
@RestController
@RequestMapping("/api/users")
@CrossOrigin(origins = "*")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<String> login(@RequestParam String username, 
                                @RequestParam String password) {
        return userService.login(username, password);
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result<User> register(@RequestBody User user) {
        return userService.register(user);
    }

    /**
     * 获取当前用户信息
     */
    @GetMapping("/info")
    public Result<User> getCurrentUserInfo(@RequestParam Long userId) {
        return userService.getUserInfo(userId);
    }

    /**
     * 分页查询用户列表
     */
    @GetMapping("/page")
    public Result<IPage<User>> getUserPage(@RequestParam(defaultValue = "1") Integer page,
                                           @RequestParam(defaultValue = "10") Integer size,
                                           @RequestParam(required = false) String keyword,
                                           @RequestParam(required = false) Integer status) {
        Page<User> pageParam = new Page<>(page, size);
        return userService.getUserPage(pageParam, keyword, status);
    }

    /**
     * 更新用户信息
     */
    @PutMapping("/{userId}")
    public Result<User> updateUser(@PathVariable Long userId, @RequestBody User user) {
        user.setId(userId);
        return userService.updateUser(user);
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{userId}")
    public Result<Void> deleteUser(@PathVariable Long userId) {
        return userService.deleteUser(userId);
    }

    /**
     * 修改密码
     */
    @PostMapping("/{userId}/change-password")
    public Result<Void> changePassword(@PathVariable Long userId,
                                      @RequestParam String oldPassword,
                                      @RequestParam String newPassword) {
        return userService.changePassword(userId, oldPassword, newPassword);
    }

    /**
     * 重置密码
     */
    @PostMapping("/{userId}/reset-password")
    public Result<Void> resetPassword(@PathVariable Long userId,
                                      @RequestParam String newPassword) {
        return userService.resetPassword(userId, newPassword);
    }
}